﻿@model Von.Web.Structs.HtmlCatalogInfo
@{
    ViewData["Title"] = "产品分类管理";
}
<div class="row">
    <div class="col-md-2" style="width:171px">
        <div><span id="Addinfo">请选择文档分类</span></div>
        <table id="catalogTree" title="文档分类" class="easyui-tree"></table>
        <div class="panel panel-info">
            <div class="panel-heading">分类名称</div>
            <div class="panel-body">
                <input id="catalogText" class="form-control">
                <div class="btn-group" role="toolbar">
                    <button type="button" role="group" class="btn btn-xs" onclick="saveCatalog('new')" title="添加同级">
                        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                    </button>
                    <button type="button" role="group" class="btn btn-xs" onclick="saveCatalog('child')" title="添加下级">
                        <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
                    </button>
                    <button type="button" role="group" class="btn btn-xs" onclick="saveCatalog('edit')" title="编辑">
                        <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                    </button>
                    <button type="button" role="group" class="btn btn-xs" onclick="deleteCatalog()" title="删除">
                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                    </button>
                    <button type="button" role="group" class="btn btn-xs" onclick="moveCatalog(true)" title="位置上移">
                        <span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
                    </button>
                    <button type="button" role="group" class="btn btn-xs" onclick="moveCatalog(false)" title="位置下移">
                        <span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>
                    </button>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-10">
        <div id="ContentListTitle" class="page-header" hidden><h3><span id="title"></span><small><a id="btnAdd" onclick="EditContent(this)" class="btn btn-link">添加新文档</a></small></h3></div>
        <table class="table" id="ContentList">
            <caption></caption>
            <thead>
                <tr>
                    <th>文档名称</th>
                    <th>标题</th>
                    <th>作者</th>
                    <th>来源</th>
                    <th>状态</th>
                    <th>发布时间</th>
                    <th></th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
</div>
@*编辑产品信息*@
<div class="modal fade" id="ContentEditor" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="ModalLabel">编辑文档信息</h4>
            </div>
            <div class="modal-body">
                <div class="input-group">
                    <label for="HtmlName" class="input-group-addon column-prompt">文档名称</label>
                    <input id="HtmlName" class="form-control" />
                </div>
                <div class="input-group">
                    <label for="Title" class="input-group-addon column-prompt">标题</label>
                    <input id="Title" class="form-control" />
                </div>
                <div class="input-group">
                    <label for="Autor" class="input-group-addon column-prompt">作者</label>
                    <input id="Autor" class="form-control" />
                </div>
                <div class="input-group">
                    <label for="Tags" class="input-group-addon column-prompt">标签</label>
                    <input id="Tags" class="form-control" />
                </div>
                <div class="input-group">
                    <label for="Keyword" class="input-group-addon column-prompt">关键词</label>
                    <input id="Keyword" class="form-control" />
                </div>
                <div class="input-group">
                    <label for="Src" class="input-group-addon column-prompt">来源</label>
                    <input id="Src" class="form-control" />
                </div>
                <div class="input-group">
                    <label for="Linker" class="input-group-addon column-prompt">链接</label>
                    <input id="Linker" class="form-control" />
                </div>
                <div class="form-group">
                    <label for="Params">摘要</label>
                    <textarea id="Summary" style="height:50px;" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label for="Content">网页内容</label>
                <textarea id="Content" name="Content" style="height:380px;" class="form-control"></textarea>
            </div>
            <div class="modal-footer">
                <div class="form-group">
                    <input type="hidden" id="ContentId" />
                    <input type="hidden" id="CatalogIdx" />
                    <input type="hidden" id="DisplayOrder" />
                    <input type="button" value="保存" onclick="SaveContentInfo()" class="btn btn-default" />
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var contentEditor;

    var PRoNode = "";
    function LoadContentList(node) {
        if (node != null && node != undefined) {
            PRoNode = node;
            $("#ContentListTitle").show();
            $("#catalogText").val(node.text);
            $("#ContentListTitle span").text(node.text + " 文档列表");
            $("#CatalogIdx").val(node.id);
            AjaxPostData('@Url.Ajax("ListHtmlContent")', { CatalogIdx: node.id }, function (datas) {
                von.clearTable();
                von.appendTable(datas);
            });
        }
    }
    function getCurrentNode() { return $('#catalogTree').tree('getSelected'); }
    function saveCatalog(flag) {
        var node = getCurrentNode();
        if (flag == "child" && node == null) SysWarning("尚未选择当前节点，无法添加下级。");
        else {
            var data = {
                ID: flag == "edit" ? node.id : "",
                Catalog: $("#catalogText").val(),
                PID: node == null ? "0" : (flag == "child" ? node.id : node.PID),
                Content: contentEditor.getData()
            };
            AjaxPostData('@Url.Ajax("SaveHtmlCatalog")', data,
                function (result) {
                    if (node == null) $('#catalogTree').tree('append', { data: [result] });
                    else if (flag == "new") {
                        node = $('#catalogTree').tree('getParent', node.target);
                        if (node == null) $('#catalogTree').tree('append', { parent: node.target, data: [result] });
                        $('#catalogTree').tree('append', { parent: node.target, data: [result] });
                    }
                    else if (flag == "child")
                        $('#catalogTree').tree('append', { parent: node.target, data: [result] });
                    else $('#catalogTree').tree('update', { target: node.target, text: result.Catalog });
                });
        }
    }
    function getSameCatalog(node) {
        if ($('#catalogTree').tree('getParent', node.target) == null) return $('#catalogTree').tree('getRoots');
        else return $('#catalogTree').tree('getParent', node.target).children;
    }
    function moveCatalog(isUp) {
        var node = getCurrentNode();
        if (node == null) throw new Error("尚未选择当前节点，无法移动。");
        var SameNodes = getSameCatalog(node);
        var idx = SameNodes.indexOf(node);
        if (isUp) {
            if (idx == 0) SysWarning("已经是顶级节点了，无法移动。");
            else {
                var data = { ID: node.ID, Order: idx };
                AjaxPostData('@Url.Ajax("MoveHtmlCatalog")', data,
                    function (result) {
                        var n2 = $("#catalogTree").tree("pop", $("#" +SameNodes[idx - 1].domId)[0]);
                        $("#catalogTree").tree("insert", { after:  node.target, data: n2 });
                    });
            }
        } else {
            if (SameNodes.length - 1 == idx) SysWarning("已经是最后节点了，无法移动。");
            else {
                var data = { ID: node.ID, Order: idx + 2 };
                AjaxPostData('@Url.Ajax("MoveHtmlCatalog")', data,
                    function (result) {
                        var n2 = $("#catalogTree").tree("pop", $("#"+SameNodes[idx + 1].domId)[0]);
                        $("#catalogTree").tree("insert", { before: node.target, data: n2 })
                    });
            }
        }
    }
    function deleteCatalog() {
        var node = getCurrentNode();
        var Catalogchildren = $('#catalogTree').tree('getChildren', node.target);
        if (Catalogchildren.length > 0) SysWarning("节点下包含子节点，不可删除此节点。");
        else {
            AjaxPostData('@Url.Ajax("DeleteHtmlCatalog")', { ID: node.ID },
                function (data) { $('#catalogTree').tree('remove', node.target); });
        }
    }
    //------------------------------------------------------------------------------------------------
    function EditContent(btn, ID) {
        von.selectRow(btn);
        $("#ContentId").val(ID == null ? "" : ID);
        if (ID == null) {
            von.editor.setData({});
            von.editor.display("new");
        } else {
            var jsonData = { ID: ID };
            AjaxPostData('@Url.Ajax("GetByHtmlContent")', jsonData, function (data) {
                von.editor.setData(data);
                contentEditor.setData(data["Content"] == undefined ? "" : data["Content"]);
                von.editor.display("edit");
            });
        }
    }
    function PublishContent(btn, id) {
        von.selectRow(btn);
        AjaxPostData("@Url.Ajax("PublishHtmlContent")", { 'ID': id }, function (result) { von.updateCell(4, "发布"); });
    }
    function DeleteContent(btn, id) {
        if (confirm('确定删除吗？')) {
            von.selectRow(btn);
            AjaxPostData("@Url.Ajax("DeleteHtmlContent")", { 'ID': id }, function (result) { von.deleteRow(); });
        }
    }
    function SaveContentInfo() {
        var data = { ID: $("#ContentId").val(), CatalogIdx: $("#CatalogIdx").val() };
        von.editor.getData(data);
        data["Content"] = contentEditor.getData();
        AjaxPostData("@Url.Ajax("SaveHtmlContent")", data, function (result) {
            if (von.editor.mode == "new") von.appendRow(von.getHtml(result));
            else von.replaceRow(von.getHtml(result));
            von.editor.close();
        });
    }
</script>
@Html.Partial("~/Views/Shared/_TreeScriptsPartial.cshtml")
@Html.Partial("~/Views/Shared/_EditorScriptsPartial.cshtml")

@Html.Import(Von.Web.Html.EImportSection.BodyTrailler, "HtmlCatalogInit",
    @<script>
        var von = VonGrid({
            app: "Content",
            keyName: "ID",
            fields: ["ID", "HtmlName", "Title", "Autor", "Src", "Linker", "Keyword", "Tags", "Summary"],
            displayFields: ["HtmlName", "Title", "Autor", "Src", "Status", "Publishedate"],
            onAddBtns: function (data) {
                von.addAction("", "Home", "HtmlContent/" + data.ID, "查看");
                von.addLink(data[von.keyName], "Edit" + von.app, "编辑");
                von.addLink(data[von.keyName], "Publish" + von.app, "发布");
                von.addDanger(data[von.keyName], "Delete" + von.app, "删除");
            }
        });
        $(function () {
            AjaxPostData('@Url.Ajax("TreeOfHtmlCatalog")', { ID: 0 },
                function (result) {
                    $('#catalogTree').tree({
                        data: result,
                        onClick: LoadContentList
                    });
                });
        });
        contentEditor = initHtmlDeveloperEditor("Content", "HtmlContent");
    </script>
)